﻿using KaytechSoftware.Cms.Entities.Interfaces;

namespace KaytechSoftware.Cms.Entities
{
    public class UnitOfWork : IUnitOfWork
    {
        private readonly EntitiesContext _context;
        private readonly IUnitOfWorkService _unitOfWorkService;

        public UnitOfWork(IUnitOfWorkService unitOfWorkService)
        {
            _context = new EntitiesContext();
            _unitOfWorkService = unitOfWorkService;
        }

        #region IUnitOfWork Members

        public EntitiesContext Context
        {
            get { return _context; }
        }

        public void ExecuteSqlCommand(string command, params object[] parameters)
        {
            _context.Database.ExecuteSqlCommand(command, parameters);
        }

        public void Commit()
        {
            _context.SaveChanges();
        }

        public void Dispose()
        {
            _context.Dispose();
            _unitOfWorkService.Release(this);
        }

        #endregion
    }
}
